<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/perf_insights/mappers/reduce.html">
<link rel="import" href="/perf_insights/mre/function_handle.html">
<link rel="import" href="/tracing/extras/measure/measure.html">
<link rel="import" href="/tracing/model/async_slice_group.html">

<script>
'use strict';
tr.exportTo('pi.m', function() {
  var MeasureAsyncSlice = tr.e.measure.MeasureAsyncSlice;

  function fetchSlicesInfo(slice, cost_infos) {
    var cost_info = {
      args: slice.args,
      title: slice.originalTitle,
      start: slice.start,
      duration: slice.duration,
      subSlices: []
    };
    cost_infos.push(cost_info);
    var subSlices = slice.subSlices;
    for (var i = 0; i < subSlices.length; ++i) {
      fetchSlicesInfo(subSlices[i], cost_info.subSlices);
    }
  }

  function getTraceImportCostReport(result, model) {
    var top_level_slices = [];
    for (var event of model.getDescendantEvents()) {
      if (event instanceof MeasureAsyncSlice &&
          event.viewSubGroupTitle === 'TraceImport' &&
          event.isTopLevel) {
        top_level_slices.push(event);
      }
    }
    var traceImportCostInfos = [];
    for (var i = 0; i < top_level_slices.length; ++i) {
      fetchSlicesInfo(top_level_slices[i], traceImportCostInfos);
    }

    result.addPair('trace_import_cost_info',
                   {'slices': traceImportCostInfos});
  }

  pi.FunctionRegistry.register(getTraceImportCostReport);

  return {
    getTraceImportCostReport: getTraceImportCostReport
  };
});
</script>
